﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataLoader;
using ServiceStack.Redis;
using RoomPriceEntity;

namespace RedisDBBiz
{
    public delegate void SetRedis(int hotel);

    public class RoomPriceSaveRedis1
    {

        IRedisClient redisClient;
        public RoomPriceSaveRedis1()
        {
            redisClient = CreateCacheClient();
        }

        public const string PREFIX = "";
        public void RedisSetRoomPrice_HASH_Month_1(int hotel)
        {
            PriceLoader dl = new PriceLoader();
            var list = dl.GetPriceList(hotel);
            foreach (var entity in list)
            {
                string keyHash = BuildKey(entity);
                redisClient.SetEntryInHash(keyHash, "P" + entity.effectdate.ToString("dd"), entity.Price.ToString());
                redisClient.SetEntryInHash(keyHash, "C" + entity.effectdate.ToString("dd"), entity.Cost.ToString());
            }
        }

        public void RedisSetRoomPrice_KV_1(int hotel)
        {
            PriceLoader dl = new PriceLoader();
            var list = dl.GetPriceList(hotel);
            foreach (var entity in list)
            {
                string keyPrice = BuildKey(entity, "P");
                redisClient.Set(keyPrice, entity.Price.ToString());
                string keyCost = BuildKey(entity, "C");
                redisClient.Set(keyCost, entity.Cost.ToString());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hotel"></param>
        public void RedisSetRoomPrice_List1(int hotel)
        {
            PriceLoader dl = new PriceLoader();
            var list = dl.GetPriceList(hotel);
            double tempPrice = 0;
            string tempEffectDate = string.Empty;
            var orderbylist = list.OrderBy(p => p.Room).ThenBy(p => p.effectdate);
            foreach (var entity in orderbylist)
            {
                if (tempPrice == 0 || tempPrice != entity.Price)
                {
                    tempEffectDate = entity.effectdate.ToString("yyyyMMdd");
                }
                string keyPrice = BuildKey(entity.Room, tempEffectDate, "P3");
                redisClient.PushItemToList(keyPrice, entity.Price.ToString());

                tempPrice = entity.Price;
            }

            //double tempCost = 0;
            //tempEffectDate = string.Empty;
            //foreach (var entity in orderbylist)
            //{
            //    if (tempCost == 0 || tempCost != entity.Cost)
            //    {
            //        tempEffectDate = entity.effectdate.ToString("yyyyMMdd");
            //    }
            //    string keyPrice = BuildKey(entity.Room, tempEffectDate, "C");
            //    redisClient.PushItemToList(keyPrice, entity.Cost.ToString());

            //    tempCost = entity.Cost;
            //}
        }

        public void RedisSetRoomPrice_List2(int hotel)
        {
            PriceLoader dl = new PriceLoader();
            var list = dl.GetPriceList(hotel);
            double tempPrice = 0;
            string tempEffectDate = string.Empty;
            var orderbylist = list.OrderBy(p => p.Room).ThenBy(p => p.effectdate);
            int count = 0;
            foreach (var entity in orderbylist)
            {
                if (tempPrice == 0 || tempPrice != entity.Price)
                {
                    tempEffectDate = entity.effectdate.ToString("yyyyMMdd");
                    string keyPrice = BuildKey(entity.Room, tempEffectDate, "P");
                    string keyCost = BuildKey(entity.Room, tempEffectDate, "C");

                    redisClient.Set(keyPrice, entity.Price.ToString());
                    redisClient.Set(keyCost, entity.Cost.ToString());
                }
                else
                {
                    string keyLength = BuildKey(entity.Room, tempEffectDate, "L");
                    redisClient.IncrementValue(keyLength);
                }
                
                

                tempPrice = entity.Price;
            }
        }

        public void RedisSetRoomPrice_Group(int GroupID, int topNumber, SetRedis setRedisDelegate)
        {
            //PriceLoader dl = new PriceLoader();
            //var list = dl.GetHotelList(GroupID, topNumber);
            //redisClient.AddRangeToList("hotelList", list);
            //redisClient.

            RoomPriceGetRedis getRedis = new RoomPriceGetRedis();
            var list = getRedis.GetList("hotelList");
            foreach (var hotel in list)
            {
                setRedisDelegate(int.Parse(hotel));
            }
        }


        public IRedisClient CreateCacheClient()
        {
            var client = new ServiceStack.Redis.BasicRedisClientManager("localhost").GetClient();
            return client;
        }

        public string BuildKey(PriceEntity pe)
        {
            StringBuilder sb = new StringBuilder();
            return sb.Append(PREFIX)
                .Append(":").Append(pe.Room)
                .Append(":").Append(pe.effectdate.ToString("yyyyMM")).ToString();
        }

        public string BuildKey(PriceEntity pe, string ColumName)
        {
            StringBuilder sb = new StringBuilder();
            return sb.Append(PREFIX)
                .Append(":").Append(pe.Room)
                .Append(":").Append(pe.effectdate.ToString("yyyyMMdd"))
                .Append(":").Append(ColumName).ToString();
        }

        public string BuildKey(int Room, string effectdate, string ColumName)
        {
            StringBuilder sb = new StringBuilder();
            return sb.Append(PREFIX)
                .Append(":").Append(Room)
                .Append(":").Append(effectdate)
                .Append(":").Append(ColumName).ToString();
        }
    }
}
